package com.bluedot.www.core.utils;

import com.bluedot.www.core.common.GlobalException;
import com.bluedot.www.core.pojo.DO.HandwritingFeature;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/15 10:38
 */
public class AlgorithmHandler {
    /**
     * 预处理算法
     *
     * @param bufferedImage 原图片
     * @return java.awt.image.BufferedImage
     * @author GUOZHIPENG
     * @date 2021/8/15 10:43
     */
    public static BufferedImage preHandleImage(BufferedImage bufferedImage) {

        try {
            bufferedImage = ImageUtil.grayImage(bufferedImage);
            bufferedImage = ImageUtil.binaryImage(bufferedImage);
            bufferedImage = ImageUtil.denoise(bufferedImage);
            return bufferedImage;
        } catch (Exception e) {
            throw new GlobalException("图片预处理异常", e);
        }
    }

    /**
     * 处理算法
     *
     * @param bufferedImage
     * @return java.awt.image.BufferedImage
     * @author GUOZHIPENG
     * @date 2021/8/15 10:43
     */
    public static HandwritingFeature analyzeHandleImage(BufferedImage bufferedImage) throws IOException {


        //        最大字面积
        float Maximum_word_area = Float.MIN_VALUE;
//        最小字面积
        float Minimum_word_area = Float.MAX_VALUE;

        float sumArea = 0;
//        像素数量
        float pixelSize = bufferedImage.getHeight() * bufferedImage.getWidth() / 1000;
        ImageTextCut imageTextCut = new ImageTextCut();
        List<BufferedImage> list = new ArrayList<>();
        List<List<BufferedImage>> Image = imageTextCut.cutToImg(bufferedImage);
        List<Integer> list2 = ImageTextCut.Line_spacing;
        //页眉留白
        float header_blank = list2.get(0);
        //页脚留白
        float footer_blank = bufferedImage.getHeight() - list2.get(list2.size() - 1);
        //标首距
        float the_first_pitch;
        if(list2.size()<3){
            the_first_pitch=0;
        }else {
            the_first_pitch= list2.get(2) - list2.get(1);
        }

        List<Integer> list3 = new ArrayList();
        for (int i = 1; i < list2.size(); i++) {
            list3.add(list2.get(i) - list2.get(i - 1));
        }
//        平均行间距
        float mean_row_spacing;
        //        最小行间距
        float minimum_ros_spacing = Float.MAX_VALUE;
        //        最大行间距
        float maximum_row_spacing = Float.MIN_VALUE;
        float temp_sum = 0;
        for (int i = 0; i < list3.size(); i++) {
            minimum_ros_spacing = Math.min(minimum_ros_spacing, list3.get(i));
            maximum_row_spacing = Math.max(maximum_row_spacing, list3.get(i));
            temp_sum = temp_sum + list3.get(i);
        }
        mean_row_spacing = temp_sum / list3.size();
        float sum = 0;
        for (BufferedImage b : Image.get(0)) {
            sum += ImageUtil.getPixelSize(b);
        }
        //        标题字体大小
        float titleSize = sum / (Image.get(0).size());
        List<Integer> list4 = ImageTextCut.word_spacing;
        // 标题字间距
        float title_word_spacing;
        if(list4.size()<3){
            title_word_spacing=0;
        }else {
            title_word_spacing= list4.get(2) - list4.get(1);
        }

        //左侧留白
        float left_blank;
        float right_blank;
        if(list4.size()<1){
            left_blank=0;
            right_blank=0;
        }else {
            left_blank= list4.get(0);
            right_blank= bufferedImage.getWidth() - list4.get(list4.size() - 1);
        }

        //右侧留白

//        占用率
        float occupancyRate = ImageUtil.getPixelSize(bufferedImage) / (bufferedImage.getWidth() * bufferedImage.getHeight());
//        字高宽比
        //最小高宽比
        float minimum_aspect_ratio = Float.MAX_VALUE;
        //最大高宽比
        float maximum_aspect_ratio = Float.MIN_VALUE;

        List<Float> list1 = ImageTextCut.Word_aspect_ratio;
        //标题高宽比
        float title_aspect_ratio = list1.get(0);
        float temp = 0;
        for (float f : list1
        ) {
            minimum_aspect_ratio = Math.min(minimum_aspect_ratio, f);
            maximum_aspect_ratio = Math.max(maximum_aspect_ratio, f);
            temp += f;
        }
        //平均高宽比
        float average_aspect_ratio = temp / list1.size();


        for (int i = 0; i < Image.size(); i++) {
            list.addAll(Image.get(i));
        }
        for (BufferedImage image : list) {
//            字面积
            float word_area = image.getWidth() * image.getHeight();
            Maximum_word_area = Math.max(Maximum_word_area, word_area);
            Minimum_word_area = Math.min(Minimum_word_area, word_area);
            sumArea += word_area;
            float pixelRatio = ImageUtil.getPixelRatio(image);

        }
//        平均字面积
        float Average_word_area = sumArea / list.size();
        ImageTextCut.Word_aspect_ratio.clear();
        ImageTextCut.Line_spacing.clear();
        ImageTextCut.word_spacing.clear();
        HandwritingFeature h = new HandwritingFeature();
        h.setPixelCount(pixelSize);
        h.setHeaderFontSize(titleSize);
        h.setMeanWordArea(Average_word_area);
        h.setMaximumFontArea(Maximum_word_area);
        h.setMinimumFontArea(Minimum_word_area);
        h.setAverageAspectRatio(average_aspect_ratio);
        h.setMeanRowSpacing(mean_row_spacing);
        h.setMinimumRowSpacing(minimum_ros_spacing);
        h.setMaximumRowSpacing(maximum_row_spacing);
        h.setTitleWordSpacing(title_word_spacing);
        h.setMinimumAspectRatio(minimum_aspect_ratio);
        h.setMaximumAspectRatio(maximum_aspect_ratio);
        h.setOccupancyRate(occupancyRate);
        h.setTheFirstPitch(the_first_pitch);
        h.setTitleAspectRatio(title_aspect_ratio);
        h.setHeaderBlank(header_blank);
        h.setFooterBlank(footer_blank);
        h.setLeftBlank(left_blank);
        h.setRightBlank(right_blank);
        return h;
    }

    public static void main(String[] args) throws IOException {
        File files=new File("C:\\Users\\白夜\\BlueDotProject\\handwritingAnalysisSys\\web\\WEB-INF\\img");
        File[] file=files.listFiles();
        for (File f:file) {
            BufferedImage bu = ImageIO.read(f);
            bu=preHandleImage(bu);
            HandwritingFeature h = analyzeHandleImage(bu);
            System.out.println(f.getName()+"-----------"+h.toString());
        }

    }
}
